const app = getApp()
Page({
    data: {
        shareCode:'',//邀请码
        sessionKey:'',
        wxOpenId:'',
    },
    onLoad (options) {
        // if(wx.getStorageSync('wxOpenId')){
        //     app.utilJs.jumpTo('/pages/center/center')
        // }
        /* 有邀请码 */
        if(JSON.stringify(options) != "{}"){
            this.setData({ shareCode:options.shareCode })
        }
        this.init();
    },
    init () {
        let that = this;
        wx.login({
            success(res) {
                let code = res.code;
                app.postRequest('/login/wxLogin',{code:code}).then(r => {
                    if(r.code == 0){
                        wx.setStorageSync("wxOpenId", r.data.wxOpenId);
                        wx.setStorageSync("sessionKey", r.data.sessionKey);
                        that.setData({ sessionKey:r.data.sessionKey, wxOpenId:r.data.wxOpenId });
                    }else{
                        app.dialogJs.tip(r.message)
                    }
                })
            }
        })
    },
    /* 用户授权 获取信息*/
    getUserInfo(e) {
        let that = this;
        wx.getSetting({
            success:(res) => {
                if(res.authSetting['scope.userInfo']){
                    wx.getUserInfo({
                        success: r => {
                            /* 用户信息 */
                            that.setData({ facePic:r.userInfo.avatarUrl, gender:r.userInfo.gender })
                            /* 验证邀请码 */
                            that.inviteMsg();
                        }
                    })
                }
            }
        })
    },
    /*验证邀请码*/
    inviteMsg () {
        let params = { shareCode:this.data.shareCode, wxOpenId:this.data.wxOpenId };
        app.getRequest('/user/shareCodeCheck', params).then( r => {
            if(r.code == 0){
                app.utilJs.jumpTo('/pages/login/login')
            }else{
                app.dialogJs.tip(r.message)
            }
        })
    }
})
